package com.changzhi.agora;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject;
import com.changzhi.common.enums.LangEnum;
import lombok.extern.slf4j.Slf4j;

import java.util.Base64;

@Slf4j
public class AgoraUtil {

    public static void peerMessages(String userId,String destinationUserId,String text) {
        //10000 1458316386295087104
        //我的 1453637198824865792
        //万里 1441282337215086592
        String user_id = userId;
        String destination = destinationUserId;

        // 客户 ID
        final String customerKey = "2ca07a82d6ac4f7b97192115d98eb712";
        // 客户密钥
        final String customerSecret = "cf325d1bd67f447a879a901858238f0b";

        // 拼接客户 ID 和客户密钥并使用 base64 编码
        String plainCredentials = customerKey + ":" + customerSecret;
        String base64Credentials = new String(Base64.getEncoder().encode(plainCredentials.getBytes()));
        // 创建 authorization header
        String authorizationHeader = "Basic " + base64Credentials;
        String authorization = HttpRequest.get("https://api.agora.io/dev/v1/projects")
                .header("Authorization", authorizationHeader)
                .header("Content-Type", "application/json")
                .execute().body();
        System.out.println("-----Basic HTTP 认证-----"+authorization);

        RtmTokenBuilder token = new RtmTokenBuilder();
        String appId = "a31ef19a0d3740d4898d38c19e85a4b9";
        String appCertificate = "54074b6e61d54811bff7f6062daec893";
        int expireTimestamp = 0;
        String tokenHeader = null;
        try {
            tokenHeader = token.buildToken(appId, appCertificate, user_id, RtmTokenBuilder.Role.Rtm_User, expireTimestamp);
        } catch (Exception e) {
            log.error("=====服务端生成的 RTM Token====={}",e.getMessage());
            e.printStackTrace();
        }

//        String user_events = HttpRequest.get("https://api.agora.io/dev/v2/project/"+appId+"/rtm/vendor/user_events")
//                .header("x-agora-token", tokenHeader)
//                .header("x-agora-uid", user_id)
//                .execute().body();
//        System.out.println("----- Token 认证 获取用户事件-----"+user_events);

        JSONObject textObject = new JSONObject();
        textObject.put("warning","");
        textObject.put("text",text);
        String payload = "/ptxx/" + textObject.toJSONString();

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("destination",destination);
        jsonObject.put("enable_offline_messaging",false);
        jsonObject.put("enable_historical_messaging",false);
        jsonObject.put("payload",payload);
        String jsonString = jsonObject.toJSONString();

        log.info("======jsonString======{}",jsonString);

        String peer_messages = HttpRequest.post("https://api.agora.io/dev/v2/project/" + appId + "/rtm/users/" + user_id + "/peer_messages?wait_for_ack=true")
                .header("Authorization", authorization)
                .header("Content-Type", "application/json;charset=utf-8")
                .header("x-agora-token", tokenHeader)
                .header("x-agora-uid", user_id)
                .body(jsonString)
                .execute().body();

        log.info("======发送点对点消息======{}",peer_messages);
    }

}
